/**
 * Created by zhouli on 18/1/17
 * Email li.zhou@huilianyi.com
 */
import React from 'react';
import {messages} from "share/common";
import {Button, Table, Badge} from 'antd';
import SearchArea from 'components/search-area.js';
import menuRoute from 'routes/menuRoute';
import 'styles/setting/person-group/person-group.scss';
import PGService from 'containers/setting/person-group/person-group.service';
import ResizeTable from "components/resize-table";

class PersonGroup extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      data: [],
      searchParams: {
        personGroupName: ""
      },
      pagination: {
        page: 0,
        total: 0,
        pageSize: 10,
        showSizeChanger: true,
        showQuickJumper: true,
      },
      searchForm: [
        {type: 'input', id: 'personGroupName', defaultValue: "", label: messages('setting.key833'/*人员组名称*/)},
      ],
      columns: [
        {
          title: messages('setting.key820'/*人员组代码*/), key: "code", dataIndex: 'code'
        },
        {
          title: messages('setting.key823'/*人员组名称*/), key: "name", dataIndex: 'name'
        },
        {
          title: messages('setting.key186'/*描述*/), key: "comment", dataIndex: 'comment'
        },
        {
          title: messages('common.column.status'),
          key: 'status',
          width: '10%',
          dataIndex: 'enabled',
          render: enabled => (
            <Badge status={enabled ? 'success' : 'error'}
                   text={
                     enabled ? messages('common.status.enable') : messages('common.status.disable')
                   }/>
          )
        },
        {
          title: messages('common.operation'),//"操作",
          dataIndex: "id",
          key: "id",
          render: (text, record) => (
            <span>
              <a onClick={(e) => this.editItemUserGroup(record)}>
                {messages('common.edit')}
              </a>
              &nbsp;&nbsp;&nbsp;
              <a onClick={(e) => this.handleRowClick(record)}>
                {messages('common.detail')}
              </a>
            </span>)
        }
      ],
    };
  }

  componentDidMount() {
    //记住页码
    let _pagination = this.getBeforePage();
    let pagination = this.state.pagination;
    pagination.page = _pagination.page;
    pagination.current = _pagination.page + 1;
    this.setState({
      pagination,
    }, () => {
      this.clearBeforePage();
      this.getList();
    })
  }

  //获取人员组数据
  getList() {
    let searchParams = this.state.searchParams;
    let pagination = this.state.pagination;

    let params = {
      name: searchParams.personGroupName,
      page: this.state.pagination.page,
      size: this.state.pagination.pageSize
    };
    PGService.getPersonGroupList(params)
      .then((data) => {
        pagination.total = data.total;
        this.setState({
          data: data.data,
          pagination: pagination,
          loading: false
        })
      })
      .catch((err) => {
        this.setState({
          loading: false
        })
      });
  };

  // 点击搜索
  handleSearch = (values) => {
    this.setState({
      searchParams: values,
      loading: true,
    }, () => {
      this.getList();
    })
  };

  // 分页点击
  onChangePager = (pagination) => {
    this.setState({
      pagination: {
        page: pagination.current - 1,
        pageSize: pagination.pageSize,
        total: pagination.total,
        current: pagination.current
      }
    }, () => {
      this.getList();
    })
  };

  // 点击行，进入该行详情页面
  handleRowClick = (record) => {
    this.setBeforePage(this.state.pagination);
    this.context.router.push(menuRoute.getMenuItemByAttr('person-group', 'key')
      .children.personGroupDetail.url.replace(':oid', record.userGroupOID).replace(':id', record.id));
  };
  // 新建人员组
  handleCreate = () => {
    this.context.router.push(menuRoute.getMenuItemByAttr('person-group', 'key').children.newPersonGroup.url);
  };
  //编辑人员组
  editItemUserGroup = (record) => {
    this.setBeforePage(this.state.pagination);
    this.context.router.push(menuRoute.getMenuItemByAttr('person-group', 'key')
      .children.editPersonGroup.url.replace(':oid', record.userGroupOID));
  };
  // 刷新人员组
  handleRefresh = () => {
    this.setState({
      loading: true
    })
    const {pagination} = this.state;
    PGService.refreshPersonGroup()
      .then((data) => {
        pagination.page = 0;
        pagination.current = 1;
        this.setState({
          pagination
        }, () => {
          this.getList();
        });
      })
      .catch((err) => {
        this.setState({
          loading: false
        });
      });
  };
  render() {
    const {searchForm, loading, data, columns, pagination} = this.state;
    return (
      <div className="budget-structure">
        <SearchArea searchForm={searchForm} submitHandle={this.handleSearch}/>
        <div className="table-header">
          <div className="table-header-title">
            {messages('common.total', {total: `${pagination.total}`})}
          </div>
          <div className="table-header-buttons">
            <Button type="primary" onClick={this.handleCreate}>
              {messages('common.create')}
            </Button>
            &nbsp;&nbsp;&nbsp;
            <Button type="primary" onClick={this.handleRefresh}>
              {messages('help-center-user.key21')/*刷新*/}
            </Button>
          </div>
        </div>
        <ResizeTable
          rowKey={record => record.id}
          loading={loading}
          dataSource={data}
          columns={columns}
          pagination={pagination}
          onChange={this.onChangePager}
          size="middle"
          bordered
        />
      </div>
    );
  }
}

PersonGroup.contextTypes = {
  router: React.PropTypes.object
};

export default PersonGroup;
